package com.codefish.framework.spring.context.annotation;

import com.codefish.framework.spring.beans.factory.support.BeanDefinition;
import com.codefish.framework.spring.beans.factory.support.IBeanDefinitionRegistry;
import com.codefish.framework.spring.core.utils.ClassUtils;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 扫描器，扫描指定包下及其子包下所有注解了{@link com.codefish.framework.spring.context.stereotype.Component} 的类并生成BeanDefinition
 *
 * @author <a href='https://gitee.com/hzf2281'>codefish</a>
 * @version 1.0.0
 * @since 2022/11/05 下午 12:57
 */
public abstract class ClassPathScanningCandidateComponentProvider {


    public Set<BeanDefinition> scanCandidateComponent(String basePackage) {
        Set<BeanDefinition> components = new HashSet<>();
        //获得指定包下所有的class对象
        List<Class<?>> classList = ClassUtils.scanClasses(basePackage);
        for (Class<?> clazz : classList) {
            //从尝试从class对象获得BeanDefinition对象并加入列表中
            BeanDefinition beanDefinition = generateBeanDefinition(clazz);
            if (beanDefinition != null) {
                components.add(beanDefinition);
            }
        }
        return components;

    }

    /**
     * 从class对象生成BeanDefinition
     *
     * @param clazz 目标class对象
     * @return 生成的bd
     */
    protected abstract BeanDefinition generateBeanDefinition(Class<?> clazz);

    /**
     * 获取BeanDefinitionRegistry
     * @return 目标BeanDefinitionRegistry
     */
    public abstract IBeanDefinitionRegistry getRegistry();

}
